<?php

namespace app\manystore\controller;

use app\common\controller\ManystoreBase;

/**
 * 团购管理
 *
 * @icon fa fa-circle-o
 */
class Certificate extends ManystoreBase
{

    /**
     * Certificate模型对象
     * @var \app\manystore\model\Certificate
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\manystore\model\Certificate;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function import()
    {
        parent::import();
    }
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)
                ->where(['is_del' => 0])
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    public function edit($ids="")
    {

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $where['id']=$ids;
                $params['status']=0;
                $this->model->save($params, $where);
                $this->success();

            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return parent::edit($ids);
    }
    public function del($ids="")
    {
        $where['id']=$ids;
        $data['is_del']=1;
        $this->model->where($where)->update($data);
        $this->success('成功');
    }
    public function category()
    {
        $Category = model('app\common\model\Category');

        if($this->request->request("keyValue")){
           $keyValue=$this->request->request("keyValue");
           $where['id']=$keyValue;
           $find=$Category->where($where)->find();
            return ['total'=>1, 'list'=>[
                ['id'=>$find['id'], 'name'=>$find['name']]
            ]
            ];
        }

        $shop_find=db('manystore_shop')->where(['id'=>SHOP_ID,'status'=>'1'])->find();
        switch($shop_find['type']){
            case 1:
                $where['type'] = 'food';
                break;
            case 2:
                $where['type'] = 'vote';
                break;
            case 3:
                $where['type'] = 'drink';
                break;
            case 4:
                $where['type'] = 'play';
                break;
            default:
                $where['type'] = 'food';
        }
        $list=$Category->where($where)->select();
        $result = array("total" => count($list), "rows" => $list);
        return json($result);
    }
}
